package cn.community.care.module.system.controller.admin.task;

import cn.community.care.framework.security.core.util.SecurityFrameworkUtils;
import cn.community.care.module.system.controller.admin.task.vo.TaskPageReqVO;
import cn.community.care.module.system.controller.admin.task.vo.TaskRespVO;
import cn.community.care.module.system.controller.admin.task.vo.TaskSaveReqVO;
import cn.community.care.module.system.dal.dataobject.permission.UserRoleDO;
import cn.community.care.module.system.dal.dataobject.task.TaskDO;
import cn.community.care.module.system.dal.mysql.permission.UserRoleMapper;
import cn.community.care.module.system.service.task.TaskService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.community.care.framework.common.pojo.PageParam;
import cn.community.care.framework.common.pojo.PageResult;
import cn.community.care.framework.common.pojo.CommonResult;
import cn.community.care.framework.common.util.object.BeanUtils;
import static cn.community.care.framework.common.pojo.CommonResult.success;

import cn.community.care.framework.excel.core.util.ExcelUtils;

import cn.community.care.framework.operatelog.core.annotations.OperateLog;
import static cn.community.care.framework.operatelog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 任务")
@RestController
@RequestMapping("/task")
@Validated
public class TaskController {

    @Resource
    private TaskService taskService;

    @Resource
    private UserRoleMapper userRoleMapper;

    @PostMapping("/create")
    @Operation(summary = "创建任务")
    public CommonResult<Long> createTask(@Valid @RequestBody TaskSaveReqVO createReqVO) {
        return success(taskService.createTask(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新任务")
    public CommonResult<Boolean> updateTask(@Valid @RequestBody TaskSaveReqVO updateReqVO) {
        taskService.updateTask(updateReqVO);
        return success(true);
    }

    @PutMapping("/fenpei")
    @Operation(summary = "更新任务")
    @PreAuthorize("@ss.hasAnyRoles('super_admin')")
    public CommonResult<Boolean> fenpei(@Valid @RequestBody TaskSaveReqVO updateReqVO) {
        taskService.fenpei(updateReqVO);
        return success(true);
    }



    @DeleteMapping("/delete")
    @Operation(summary = "删除任务")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteTask(@RequestParam("id") Long id) {
        taskService.deleteTask(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得任务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<TaskRespVO> getTask(@RequestParam("id") Long id) {
        TaskDO task = taskService.getTask(id);
        return success(BeanUtils.toBean(task, TaskRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得任务分页")
    public CommonResult<PageResult<TaskRespVO>> getTaskPage(@Valid TaskPageReqVO pageReqVO) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        List<UserRoleDO> userRoleDOS = userRoleMapper.selectList(UserRoleDO::getUserId, userId);
        for (UserRoleDO userRoleDO : userRoleDOS) {
            if (userRoleDO.getRoleId() == 1){
                pageReqVO.setAssigneeId(null);
                break;
            }
            if (userRoleDO.getRoleId() == 156){
                pageReqVO.setAssigneeId(userId);
                break;
            }
        }

        PageResult<TaskDO> pageResult = taskService.getTaskPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TaskRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得任务分页")
    public CommonResult<PageResult<TaskRespVO>> getTaskList(@Valid TaskPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<TaskDO> pageResult = taskService.getTaskPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TaskRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出任务 Excel")
    @OperateLog(type = EXPORT)
    public void exportTaskExcel(@Valid TaskPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TaskDO> list = taskService.getTaskPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "任务.xls", "数据", TaskRespVO.class,
                        BeanUtils.toBean(list, TaskRespVO.class));
    }

}